import request from '@/utils/request'

export function formattedNumber(price) {
    if (!price) {
        return
    }
    var number = price.toLocaleString();
    return number
}

export function objectFun(value = '', arr = [], lvalue = 'dictValue', llabel = 'dictLabel') {
    if (!value || !arr || !arr.length) {
        return
    }
    var name = '';
    for (const item of arr) {
        if (item[lvalue] == value) {
            name = item[llabel];
        }
    }
    return name;
}

export function checkUrl(url) {
    if (!url) {
        return '';
    }
    if (url.substring(0, 4) == "http") {
        return url;
    } else {
        url = 'https://' + url;
        return url;
    }
}


export function priceSwitch(x = 0) {
    if (!x) {
        x = 0;
    }
    var f = parseFloat(x);
    if (isNaN(f)) return false;
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length < (rs + 1) + 2) {
        s += '0';
    }
    var leftNum = s.split(".")[0];
    var rightNum = "." + s.split(".")[1];
    var result;
    var resultArray = new Array();
    if (leftNum.length > 3) {
        var i = true;
        while (i) {
            resultArray.push(leftNum.slice(-3));
            leftNum = leftNum.slice(0, leftNum.length - 3);
            if (leftNum.length < 4) {
                i = false;
            }
        }
        var sortArray = new Array();
        for (var i = resultArray.length - 1; i >= 0; i--) {
            sortArray.push(resultArray[i]);
        }
        result = leftNum + "," + sortArray.join(",") + rightNum;
    } else {
        result = s;
    }
    return result;
}

export function timestampToTime(timestamp) {
    if (!timestamp) {
        return 0
    }
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + '/' + M + '/' + D + ' ' + h + ':' + m + ':' + s;
}

export function happenTimeFun(num, types = "yyyy-MM-dd hh:ii", dd = 1) {
    if (!num) {
        return '';
    }
    let zthisTime = num;
    if (dd == 1) {
        zthisTime = num + "";
        zthisTime = zthisTime.replace(/-/g, '/');

    }
    let date = new Date(zthisTime);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    if (types == 'yyyy-MM-dd hh:ii:ss') {
        return y + '/' + MM + '/' + d + ' ' + h + ':' + m + ':' + s;
    } else if (types == 'hh:ii') {
        return h + ':' + m;
    } else if (types == 'yyyy-MM-dd_text') {
        return `${y}年${MM}月${d}日`;
    } else if (types == 'yyyy-MM-dd_text_hh_mm') {
        return `${y}年${MM}月${d}日${h}:${m}`;
    } else if (types == 'yyyy/MM/dd') {
        return `${y}/${MM}/${d}`;
    } else if (types == 'yyyy-MM-dd') {
        return `${y}-${MM}-${d}`;
    } else if (types == 'MM/dd') {
        return `${MM}/${d}`;
    } else if (types == 'yyyy-MM') {
        return `${y}-${MM}`;
    } else if (types == 'MM/dd hh:ii') {
        return `${MM}/${d} ${h}:${m}`;
    } else {
        return `${y}/${MM}/${d} ${h}:${m}`;
    }
}


export function getCardInfo(accountNo) {
  return request({
    url: '/api/card/' + accountNo,
    method: 'get'
  })
}